import numpy as np
from jqdatasdk import *
from jqdatasdk.technical_analysis import *

import copy
from ZIndex.tool import Util
import pandas as pd
import sign as sign

# auth('18727012661','Cptbtptp123456')
auth('18995532338','Znufe2006@')

# display_name = get_security_info('000001.XSHE').display_name
# 'sz.300008', 'sz.002623', 'sz.000665', 'sz.002411', 'sz.300222'
# print(display_name)
# security_list = ['sz.300008', 'sz.002623', 'sz.000665', 'sz.002411', 'sz.300222','sz.002432','sz.002031','sz.300456','sz.300933'
#                 ,'sz.002455','sh.600737','sz.300017','sh.600506','sh.601001','sz.300030','sh.603881','sz.000651']
security_list = ['sz.300008', 'sz.002623', 'sz.000665', 'sz.002411', 'sz.300222','sz.002432','sz.002031','sz.300456','sz.300933'
                ,'sz.002455','sh.600737','sz.300017','sh.600506','sh.603881']
security_list = normalize_code(security_list)
print(security_list)
# index_list = ['KDJ_S','LWR_S','MACD_S','BRAR_S', 'LB_S','VRSI_S','MA_S','BOLL_S','OBV_S','MFI_S']
index_list = ['KDJ_S','LWR_S','MACD_S','BRAR_S', 'LB_S','VRSI_S','BOLL_S','OBV_S','MFI_S']

stock = pd.DataFrame()
# 从待选指标中不重复抽样5个指标
# 随机指标个数
select_num = len(index_list)
select_index = Util.listRandomChoice(index_list, select_num)


for i in np.arange(30):
    # check_date = util.getNowDate()
    check_date = Util.getYesterday(int(i) + 1)
    print("测试时间为：" + str(check_date))

    # 获取前五日该指标值，从而找到该指标知期趋势
    # TODO 这个地方有个问题，就是如果是节假日，该数据可能获取不准，
    check_date_before = Util.getYesterday(int(i) + 5)

    # 获取前一个交易日价格数据
    result = get_price(security_list, end_date=check_date, count=1, frequency='daily', \
                       fields=['open', 'close', 'high', 'low', 'volume', 'money'], panel=True)
    # print(result)
    # security_list = result['code'].tolist()

    # print(result)
    ### 参数设置 ###
    # KDJ 统计的天数
    KDJ_N, KDJ_M1, KDJ_M2 = 9, 3, 3
    WR_N, WR_N1, WR_N2 = 10, 6, 3
    MACD_MID, MACD_LONG, MACD_SHORT, MACD_LONG2, MACD_LONG3 = 12, 26, 9, 40, 62
    BRAR_N = 26
    CYR_N, CYR_M = 13, 5
    MACD_M1, MACD_M2 = 5, 10
    VRSI_M1, VRSI_M2, VRSI_M3 = 6, 12, 24
    MA_N = 5
    MA_LONG_N = 15
    BOLL_N, BOLL_M = 11, 5
    Boll_timeperiod, Boll_nbdevup,Boll_nbdevdn = 20, 2, 2
    OBV_TimePeriod = 30
    VOL_M1, VOL_M2 = 5, 20
    MFI_TimePeriod = 14
    ### 参数设置 end ###

    # 获取KDJ指标 返回K D J的值
    K, D, J = KDJ(security_list, check_date=check_date, N=KDJ_N, M1=KDJ_M1, M2=KDJ_M2)
    result['K'] = K.values()
    result['D'] = D.values()
    result['J'] = J.values()

    KDJ_sign = sign.KDJ_sign(result)
    result['KDJ_S'] = KDJ_sign.values()

    # 获取 LWR指标，返回LWR1， LWR2的值
    LWR1, LWR2 = LWR(security_list, check_date=check_date, N=WR_N, M1=WR_N1, M2=WR_N2)
    result['LWR1'] = LWR1.values()
    result['LWR2'] = LWR2.values()
    WR_sign = sign.WR_Sign(result)
    result['LWR_S'] = WR_sign.values()

    # 获取MACD指标
    macd_dif, macd_dea, macd_macd = MACD(security_list, check_date=check_date, SHORT=MACD_SHORT, LONG=MACD_LONG,
                                         MID=MACD_MID)
    result['macd_dif'] = macd_dif.values()
    result['macd_dea'] = macd_dea.values()
    result['macd_macd'] = macd_macd.values()
    MACD_sign = sign.MACD_sign(result)
    result['MACD_S'] = MACD_sign.values()

    # 获取 BRAR-情绪指标，AR, BR VR CY
    BR, AR = BRAR(security_list, check_date=check_date, N=BRAR_N)
    result['BR'] = BR.values()
    result['AR'] = AR.values()
    CR1, MA1, MA2, MA3, MA4 = CR(security_list, check_date=check_date, N=MACD_LONG, M1=MACD_SHORT, M2=MACD_MID,
                                 M3=MACD_LONG2, M4=MACD_LONG3)
    result['CR'] = CR1.values()
    result['MA1'] = MA1.values()
    result['MA2'] = MA2.values()
    result['MA3'] = MA3.values()
    result['MA4'] = MA4.values()

    VR_values, MAVR = VR(security_list, check_date=check_date, N=MACD_LONG, M=MACD_SHORT)
    result['VR'] = VR_values.values()
    BRAR_sign = sign.BRAR_Sign(result)
    result['BRAR_S'] = BRAR_sign.values()

    CYR_values, MACYR = CYR(security_list, check_date=check_date, N=CYR_N, M=CYR_M)
    result['CYR'] = CYR_values.values()
    result['MACYR'] = MACYR.values()
    CYR_Sign = sign.CYR_Sign(result)
    result['CYR_S'] = CYR_Sign.values()

    # 观察成交金额的变化，比观察成交手数更具意义，因为成交手数并未反应股价的涨跌的后所应支出的实际金额
    AMOW, AMO1, AMO2 = AMO(security_list, check_date=check_date, M1=MACD_M1, M2=MACD_M2)
    result['AMOW'] = AMOW.values()
    result['AMO1'] = AMO1.values()
    result['AMO2'] = AMO2.values()
    # 量比指股市开市后平均每分钟的成交量与过去N个交易日平均每分钟成交量之比、
    # 量比0.5成交量萎缩到极致，变盘随时发生。 量比0.8--1.5成交量处于正常水平。量比1.5--2.5温和放量，将会延续原有趋势。
    # 量比2.5--5明显放量，可以采取相应行动了。量比5--10放巨量表现，趋势已到末期。量比>10极端放量，趋势已经到默契，可以考虑反向操作。
    lb = LB(security_list, check_date=check_date, N=MACD_M2)
    result['LB'] = lb.values()
    LB_Sign = sign.LB_sign(result)
    result['LB_S'] = LB_Sign.values()

    # VRSI-相对强弱量
    VRSI1, VRSI2, VRSI3 = VRSI(security_list, check_date=check_date, N1=VRSI_M1, N2=VRSI_M2, N3=VRSI_M3)
    result['VRSI1'] = VRSI1.values()
    result['VRSI2'] = VRSI2.values()
    result['VRSI3'] = VRSI3.values()

    VRSI_Sign = sign.VRSI_Sign(result)
    result['VRSI_S'] = VRSI_Sign.values()

    # 均线
    MA_values = MA(security_list, check_date=check_date, timeperiod=MA_N)
    result['MA'] = MA_values.values()
    MA_Sign = sign.MA_sign(result)
    result['MA_S'] = MA_Sign.values()

    # 多空布林通道线
    # bbi, upr, dwn = BBIBOLL(security_list, check_date=check_date, N=BOLL_N, M=BOLL_M)
    # result['bbi'] = bbi.values()
    # result['upr'] = upr.values()
    # result['dwn'] = dwn.values()
    # BBIBOLL_Sign = sign.BBIBOLL_Sign(result)
    # result['BBIBOLL_S'] = BBIBOLL_Sign.values()

    # 布林通道线
    upperband, middleband, lowerband = Bollinger_Bands(security_list, check_date=check_date, timeperiod=Boll_timeperiod, nbdevup=Boll_nbdevup, nbdevdn=Boll_nbdevdn)
    result['upperband'] = upperband.values()
    result['middleband'] = middleband.values()
    result['lowerband'] = lowerband.values()
    BOLL_Sign = sign.BOLL_Sign(result)
    result['BOLL_S'] = BOLL_Sign.values()

    # 多空比率净额= [（收盘价－最低价）－（最高价-收盘价）] ÷（ 最高价－最低价）×V
    OBV_Sign = OBV(security_list, check_date=check_date, timeperiod=OBV_TimePeriod)
    OBV_Sign_before = OBV(security_list, check_date=check_date_before, timeperiod=OBV_TimePeriod)
    # MA_20 = MA(security_list, check_date=check_date, timeperiod=MA_LONG_N)
    VOL_Sign,MAVOL1,MAVOL2 = VOL(security_list, check_date=check_date_before, M1=VOL_M1, M2=VOL_M2)

    result['OBV'] = OBV_Sign.values()
    result['OBV_before'] = OBV_Sign_before.values()
    result['MAVOL2'] = MAVOL2.values()
    result['OBV_S'] = sign.OBV_sign(result).values()


    # MFI资金流量指标
    MFI_Sign = MFI(security_list, check_date=check_date, timeperiod=MFI_TimePeriod)
    MFI_Sign_before = MFI(security_list, check_date=check_date_before, timeperiod=MFI_TimePeriod)

    result['MFI'] = OBV_Sign.values()
    result['MFI_Sign_before'] = MFI_Sign_before.values()
    result['MFI_S'] = sign.MFI_sign(result).values()

    # result.to_csv("Sign_all.csv", mode='a', header=False)
    # result.to_csv("Sign.csv", columns=['time', 'code', 'open', 'close', 'high', 'low',
    #                                    'KDJ_S', 'LWR_S', 'MACD_S', 'BRAR_S',
    #                                    'LB_S', 'VRSI_S', 'MA_S', 'BOLL_S'], mode='a', header=False)



    # sign_df = result[['time', 'code', 'KDJ_S', 'LWR_S', 'MACD_S', 'BRAR_S',
    #                   'LB_S', 'VRSI_S', 'MA_S', 'BOLL_S', 'OBV_S','MFI_S']]


    # sign_df = result[['time', 'code', 'KDJ_S', 'LWR_S', 'MACD_S', 'BRAR_S',
    #                   'LB_S', 'VRSI_S', 'BOLL_S', 'OBV_S','MFI_S']]

    # 根据上述指标重新构建sign结果表
    result_select_index = []
    result_select_index = copy.deepcopy(select_index)
    result_select_index.insert(0, 'time')
    result_select_index.insert(1, 'code')
    result_select_index.insert(2, 'close')
    # print("result_select_index:")
    # print(result_select_index)

    sign_result_df = result.loc[:, result_select_index]

    sign_result_df['sum'] = sign_result_df[select_index].apply(lambda x: x.sum(), axis=1)

    # 统计每支股票信号个数
    print(sign_result_df)
    stock = pd.concat([stock, sign_result_df], axis=1)

    result_select_index.append('sum')
    columns = result_select_index

    result['sum'] = sign_result_df['sum']
    # columns = ['time', 'code',
    #            'KDJ_S', 'LWR_S', 'MACD_S', 'BRAR_S',
    #            'LB_S', 'VRSI_S',  'BOLL_S', 'OBV_S','MFI_S', 'sum']



    # print(columns)
    # sign 数据去重，因节假日获取为上一交易日
    stock = stock.drop_duplicates()
    result = result.drop_duplicates()
    Util.dataFrameToCsv(result, "/result/Sign_all.csv", True)

    Util.dataFrameToCsv(result, "/result/Sign.csv", True, columns=columns)

    print(stock)


























